home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 April / Chip CMCD0400.iso / SOFTWARE / Freeware / Programare / Bass / LOADNGET.C < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-23  |  5.8 KB  |  187 lines

  1. /* BASS Console Test (LL/GPA version), copyright (c) 1999 Ian Luck.
  2. ===================================================================
  3. Demonstrates how you can use LoadLibrary and GetProcAddress to import
  4. BASS, instead of using BASS.LIB. Also demonstrates including BASS.DLL
  5. in the EXE as a resource, instead of seperate as a file. It's basically
  6. the same as the CONTEST.C version, with LoadBASS and FreeBASS functions
  7. added, to import and free BASS respectively.
  8. Other source: loadnget.rc
  9. Imports: kernel32.lib, user32.lib, winmm.lib
  10. */
  11.  
  12. #include <windows.h>
  13. #include <mmsystem.h>
  14. #include <conio.h>
  15.  
  16. #define BASSDEF(f) (WINAPI *f)    // define the functions as pointers
  17. #include "bass.h"
  18.  
  19. char tempfile[MAX_PATH];    // temporary BASS.DLL
  20. HINSTANCE bass=0;            // bass handle
  21.  
  22. /* load BASS and the required functions */
  23. void LoadBASS()
  24. {
  25.     BYTE *data;
  26.     HANDLE hres,hfile;
  27.     DWORD len,c;
  28.     char temppath[MAX_PATH];
  29.     /* get the BASS.DLL resource */
  30.     if (!(hres=FindResource(GetModuleHandle(NULL),"BASS_DLL",RT_RCDATA))
  31.         || !(len=SizeofResource(NULL,hres))
  32.         || !(hres=LoadResource(NULL,hres))
  33.         || !(data=LockResource(hres))) {
  34.         printf("Error: Can't get the BASS.DLL resource\n");
  35.         ExitProcess(0);
  36.     }
  37.     /* get a temporary filename */
  38.     GetTempPath(MAX_PATH,temppath);
  39.     GetTempFileName(temppath,"bas",0,tempfile);
  40.     /* write BASS.DLL to the temporary file */
  41.     if (!(hfile=CreateFile(tempfile,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_TEMPORARY,NULL))) {
  42.         printf("Error: Can't write BASS.DLL\n");
  43.         ExitProcess(0);
  44.     }
  45.     WriteFile(hfile,data,len,&c,NULL);
  46.     CloseHandle(hfile);
  47.  
  48.     /* load the temporary BASS.DLL library */
  49.     if (!(bass=LoadLibrary(tempfile))) {
  50.         printf("Error: Can't load BASS.DLL\n");
  51.         ExitProcess(0);
  52.     }
  53.     /* "load" all the BASS functions that are to be used */
  54.     #define LOADBASSFUNCTION(f) (FARPROC)f=GetProcAddress(bass,#f)
  55.     LOADBASSFUNCTION(BASS_GetVersion);
  56.     LOADBASSFUNCTION(BASS_ErrorGetCode);
  57.     LOADBASSFUNCTION(BASS_Init);
  58.     LOADBASSFUNCTION(BASS_Free);
  59.     LOADBASSFUNCTION(BASS_GetCPU);
  60.     LOADBASSFUNCTION(BASS_Start);
  61.     LOADBASSFUNCTION(BASS_SlideVolume);
  62.     LOADBASSFUNCTION(BASS_IsSliding);
  63.     LOADBASSFUNCTION(BASS_MusicLoad);
  64.     LOADBASSFUNCTION(BASS_MusicPlay);
  65.     LOADBASSFUNCTION(BASS_StreamCreateFile);
  66.     LOADBASSFUNCTION(BASS_StreamPlay);
  67.     LOADBASSFUNCTION(BASS_ChannelIsActive);
  68.     LOADBASSFUNCTION(BASS_ChannelGetFlags);
  69.     LOADBASSFUNCTION(BASS_ChannelSetAttributes);
  70.     LOADBASSFUNCTION(BASS_ChannelGetAttributes);
  71.     LOADBASSFUNCTION(BASS_ChannelGetPosition);
  72.     LOADBASSFUNCTION(BASS_ChannelGetLevel);
  73.     LOADBASSFUNCTION(BASS_ChannelSetSync);
  74. }
  75.  
  76. /* free the BASS library from memory and delete the temporary file */
  77. void FreeBASS()
  78. {
  79.     if (!bass) return;
  80.     FreeLibrary(bass);
  81.     bass=0;
  82.     DeleteFile(tempfile);
  83. }
  84.  
  85. /* display error messages */
  86. void Error(char *text) 
  87. {
  88.     printf("Error(%d): %s\n",BASS_ErrorGetCode(),text);
  89.     BASS_Free();
  90.     FreeBASS();
  91.     ExitProcess(0);
  92. }
  93.  
  94. static DWORD starttime;
  95.  
  96. /* looping synchronizer, resets the clock */
  97. void CALLBACK LoopSync(HSYNC handle, DWORD channel, DWORD data)
  98. {
  99.     starttime=timeGetTime();    // reset the clock
  100. }
  101.  
  102. void main(int argc, char **argv)
  103. {
  104.     HMUSIC mod;
  105.     HSTREAM str;
  106.     DWORD time,pos,level;
  107.     int a,freq;
  108.     BOOL mono=FALSE;
  109.  
  110.     printf("LoadLibrary/GetProcAddress example : MOD/MP3/WAV player\n"
  111.         "-------------------------------------------------------\n");
  112.  
  113.     LoadBASS();
  114.  
  115.     /* check that BASS 0.6 was loaded... not really necessary as it was in the EXE */
  116.     if (BASS_GetVersion()!=MAKELONG(0,6)) {
  117.         printf("BASS version 0.6 was not loaded\n");
  118.         return;
  119.     }
  120.  
  121.     if (argc<2 || argc>3) {
  122.         printf("\tusage: contest <file> [low]\n\tlow = lower quality, uses less CPU.\n");
  123.         return;
  124.     }
  125.  
  126.     /* setup output - default device, 44100hz (22050 if "low" quality), stereo, 16 bits */
  127.     freq=(argv[2] && !stricmp(argv[2],"low"))?22050:44100;
  128.     if (!BASS_Init(-1,freq,0,GetForegroundWindow()))
  129.         Error("Can't initialize device");
  130.     /* try streaming the file */
  131.     if (str=BASS_StreamCreateFile(FALSE,argv[1],0,0,0)) {
  132.         /* check if the stream is mono (for the level indicator) */
  133.         mono=BASS_ChannelGetFlags(str)&BASS_SAMPLE_MONO;
  134.         BASS_ChannelSetSync(str,BASS_SYNC_END,0,&LoopSync);
  135.     } else {
  136.         /* load the MOD (with looping and normal ramping) */
  137.         if (!(mod=BASS_MusicLoad(FALSE,argv[1],0,0,BASS_MUSIC_LOOP|BASS_MUSIC_RAMP)))
  138.             /* not a MOD either */
  139.             Error("Can't play the file");
  140.         BASS_ChannelSetSync(mod,BASS_SYNC_END,0,&LoopSync);
  141.     }
  142.  
  143.     BASS_Start();
  144.     if (str)
  145.         BASS_StreamPlay(str,FALSE,BASS_SAMPLE_LOOP);
  146.     else
  147.         BASS_MusicPlay(mod);
  148.     starttime=timeGetTime();
  149.     printf("now playing... press any key to stop\n");
  150.  
  151.     /* NOTE: some compilers don't support _kbhit */
  152.     while (!_kbhit() && BASS_ChannelIsActive(str?str:mod)) {
  153.         /* display some stuff and wait a bit */
  154.         time=timeGetTime()-starttime;
  155.         level=BASS_ChannelGetLevel(str?str:mod);
  156.         pos=BASS_ChannelGetPosition(str?str:mod);
  157.         if (str)
  158.             printf("pos %09d - time %d:%02d - L ",pos,time/60000,(time/1000)%60);
  159.         else
  160.             printf("pos %03d:%03d - time %d:%02d - L ",LOWORD(pos),HIWORD(pos),time/60000,(time/1000)%60);
  161.         for (a=93;a;a=a*2/3) putchar(LOWORD(level)>=a?'*':'-');
  162.         putchar(' ');
  163.         if (mono)
  164.             for (a=1;a<128;a+=a-(a>>1)) putchar(LOWORD(level)>=a?'*':'-');
  165.         else
  166.             for (a=1;a<128;a+=a-(a>>1)) putchar(HIWORD(level)>=a?'*':'-');
  167.         printf(" R - cpu %d%%  \r",BASS_GetCPU());
  168.         Sleep(50);
  169.     }
  170.     printf("                                                                 \r");
  171.  
  172.     /* get the frequency... and wind it down */
  173.     BASS_ChannelGetAttributes(str?str:mod,&freq,NULL,NULL);
  174.     level=freq/50;
  175.     for (;freq>2000;freq-=level) {
  176.         BASS_ChannelSetAttributes(str?str:mod,freq,-1,-101);
  177.         Sleep(5);
  178.     }
  179.  
  180.     /* fade-out to avoid a "click" */
  181.     BASS_SlideVolume(0,1,BASS_SLIDE_DIGITAL);
  182.     while (BASS_IsSliding()) ;
  183.  
  184.     BASS_Free();
  185.     FreeBASS();
  186. }
  187.